<template>
  <div class="closing">
    <div>
      <el-button size="mini" @click="handleGetUntreated">{{ $t('btn.reload') }}</el-button>
      <el-button type="primary" size="mini" @click="isClosingDialog=true">{{ $t('market.filter') }}</el-button>
      <el-button type="primary" size="mini" :disabled="!calcuNewly" @click="calculate">{{ $t('market.sun') }}</el-button>
      <el-button type="primary" size="mini" @click="handleExport">{{ $t('btn.toExcel') }}</el-button>
      <el-button type="danger" size="mini" @click="delhandle">删除行记录</el-button>
    </div>
    <div class="header">
      <div class="header-name">
        <span>{{ $t('market.period') }}</span>
        <el-input v-model="period" size="mini" disabled />
      </div>
      <!--      当前关账期间-->
      <dataRow ref="mainData" />
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        sortable
        table-selection
        @selectionChange="selectionDatas"
      >
        <!--处理进度-->
        <template v-slot:btnState="col">
          <el-tag v-if="col.scope.row.fisfinished === 1" type="success">{{ $t('market.accomplish') }}</el-tag>
          <el-tag v-else type="danger">{{ $t('market.accomplishNo') }}</el-tag>
        </template>
        <!--结果状态-->
        <template v-slot:orderSlot="col">
          <el-tag v-if="col.scope.row.fissuccess === 1" type="success">{{ $t('market.succeed') }}</el-tag>
          <el-tag v-if="col.scope.row.fissuccess === 2" type="primary">{{ $t('market.sunIn') }}</el-tag>
          <el-tag v-else-if="col.scope.row.fissuccess === 0" type="danger">{{ $t('market.defeated') }}</el-tag>
        </template>
      </jc-table>
    </div>
    <!--    分页器-->
    <div class="footer">
      <jc-pagination
        v-show="total > 0"
        :total="total"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        @pagination="handleGetUntreated"
      />
    </div>
    <!-- 出库成本维护 -->
    <el-dialog
      v-dialogDrag
      :title="$t('menu.outbound')"
      model
      :visible.sync="isClosingDialog"
      :close-on-click-modal="false"
      width="600px"
      top="5vh"
      center
    >
      <div>
        <el-form :model="subDetails" size="mini" label-width="100px">
          <el-form-item :label="$t('market.checkDate')">
            <el-date-picker
              v-model="subDetails.period"
              type="month"
              value-format="yyyy-MM"
              style="width: 152px"
              placeholder="选择月"
            />
          </el-form-item>
          <el-form-item :label="$t('market.frombillNunber')">
            <el-input v-model.trim="subDetails.frombillNo" />
          </el-form-item>
          <el-form-item :label="$t('market.fmaterial')">
            <el-input v-model.trim="subDetails.materialNumber" />
          </el-form-item>
          <el-form-item :label="$t('market.stockname')">
            <el-input v-model.trim="subDetails.stockName" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isClosingDialog = false">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="initialCon">{{ $t('btn.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  calculateIssueDetail1, deleteCostmaintain,
  queryCostmaintainList
} from '@/api/financial/warehousing'
import dataRow from '@/views/financial/company/components/dataRow'
import searData from '@/components/Search/mixin'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'

export default {
  name: 'OutInbound',
  components: {
    dataRow
  },
  mixins: [searData],
  data() {
    return {
      subDetails: {
        period: '',
        stockName: '',
        materialNumber: '',
        frombillNo: ''
      },
      total: 0, // 总条目
      pageNum: 1, // 当前页
      pageSize: 200, // 每页显示多少条数据
      period: '',
      loading: false,
      isClosingDialog: false,
      calcuNewly: true, // 是否可以运算 新增成本单
      // 表头
      selectData: [],
      tableData: [],
      tableHeight: '70vh',
      tableHeader: [
        { label: this.$t('market.period'), prop: 'fdate', width: '100' },
        // { label: this.$t('market.billType'), prop: 'fbillType' },
        { label: this.$t('market.companyname'), prop: 'fcompanyname' },
        { label: this.$t('market.creator'), prop: 'fcreater' },
        { label: this.$t('market.createDate'), prop: 'fcreatedate' },
        { label: this.$t('market.progress'), type: 'state', prop: 'fisfinished' },
        { label: this.$t('market.finishTime'), prop: 'ffinishedtime' },
        { label: this.$t('market.isstate'), type: 'order', prop: 'fissuccess' },
        { label: this.$t('market.issuccess'), prop: 'feason', minWidth: '200' }
      ]
    }
  },
  created() {
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 220px)' : '73vh'
  },
  methods: {
    // 过滤确认
    initialCon() {
      this.isClosingDialog = false
      const { period } = this.subDetails
      this.period = period
      this.handleGetUntreated()
    },
    // 获取出入库成本维护运算
    async handleGetUntreated() {
      this.calcuNewly = this.$refs.mainData.panyForm.nowMaintainDate <= this.period
      this.loading = true
      if (!this.period) {
        this.isClosingDialog = true
        this.loading = false
        return
      }
      const DATA = {
        ...this.subDetails,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      const RES = await queryCostmaintainList(DATA)
      if (RES.code === 0) {
        this.loading = false
        this.tableData = RES.data.array
        this.total = RES.data.total
      } else {
        this.loading = false
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 全选
    selectionDatas(val) {
      this.selectData = []
      val.forEach(res => {
        this.selectData.push(res.ftaskId)
      })
    },
    // 运算
    calculate() {
      calculateIssueDetail1({ ...this.subDetails }).then(res => {
        if (res.code === 0) {
          this.handleGetUntreated()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 删除行记录
    delhandle() {
      const DATA = {
        ftaskId: this.selectData
      }
      deleteCostmaintain(DATA).then(res => {
        if (res.code === 0) {
          this.handleGetUntreated()
          this.$message({ showClose: true, message: res.message, type: 'success' })
        } else {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
          })
        }
      })
    },
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else {
          queryCostmaintainList({
            pageSize: 100000,
            pageNum: 1,
            ...this.subDetails
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.array
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '出入库成本维护运算')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.closing {
  @include listBom;
  .el-input::v-deep .el-input__inner {
    height: 30px;
    line-height: 30px;
  }
  .header{
    margin-top: 10px;
    .header-name{
      span {
        width: 100px;
        margin-right: 10px;
        text-align: right;
      }
    }
  }
}
</style>
